<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>Chrome 浏览器中执行 JavaScript</h1>
<p>本章节为大家介绍如何在 Chrome 浏览器上进行 JavaScript 代码的运行与调试。</p>
<p>Chrome 是由 Google 开发的免费网页浏览器，对于前端开发来说（尤其是调试代码）非常方便。</p>
<p>Chrome 官网地址：<a href="https://www.google.com/intl/zh-CN/chrome/" rel="noopener noreferrer" target="_blank">https://www.google.com/intl/zh-CN/chrome/</a>。</p>
<p>我们在 Chrome 浏览器中可以通过按下 <span class="marked">F12</span> 按钮或者右击页面，选择<strong>"检查"</strong>来开启开发者工具。 </p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2020/11/2FC7724A-4281-41C0-94F5-0DAE2D9D7C14.jpg"></p>
<p>也可以在右上角菜单栏选择 <strong>"更多工具"=》"开发者工具"</strong> 来开启：</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2020/11/62B70027-DD24-4F04-8040-2DB58D1EDCFC.jpg"></p>
<h2>1、Console 窗口调试 JavaScript 代码</h2><p>
打开开发者工具后，我们可以在 Console 窗口调试 JavaScript代码，如下图：</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2020/11/93B1E50A-D2D9-4FB4-B458-D50045FDE599.jpg"></p>
<p>上图中我们在 <span class="marked">&gt;</span> 符号后输入我们要执行的代码 <span class="marked">console.log("runoob")</span>，按回车后执行。</p>
<p>我们也可以在其他地方复制一段代码过来执行，比如复制以下代码到 Console 窗口，按回车执行：</p>
<pre class="prettyprint prettyprinted" style=""><span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"runoob-1"</span><span class="pun">)</span><span class="pln">
console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"runoob-2"</span><span class="pun">)</span><button class="copy-code-button" type="button" data-clipboard-text="console.log(&quot;runoob-1&quot;)
console.log(&quot;runoob-2&quot;)"></button></pre>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2020/11/3F6D97EB-2E51-47AA-919D-79EE62DC14DA.jpg"></p>
<p>清空 Console 窗口到内容可以按以下按钮：</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2020/11/C1A3FF50-1C1C-463E-B12C-D7D8735F6844.jpg"></p>
<hr><h2>
2、Chrome snippets 小脚本</h2>
<p>
我们也可以在 Chrome 浏览器中创建一个脚本来执行，在开发者工具中点击 Sources 面板，选择 Snippets 选项卡，在导航器中右击鼠标，然后选择 Create new snippet 来新建一个脚本文件：</p>
<p><img decoding="async" width="70%" src="https://www.runoob.com/wp-content/uploads/2020/11/8C18C75F-6C15-4B7F-8C66-122D1D23C14E.jpg"></p>
<p>如果你没看到 Snippets ，可以点下面板上到 <span class="marked">&gt;&gt;</span> 就能看到了。</p>
<p><img decoding="async" width="70%" src="https://www.runoob.com/wp-content/uploads/2020/11/9F5F8D84-9C0D-4F6B-98AF-8B9349118297.jpg"></p><p>

点击 Create new snippet 后，会自动创建一个文件，你只需在右侧窗口输入以下代码，然后按 Command+S（Mac）或 Ctrl+S（Windows 和 Linux）保存更改即可。</p>
<pre class="prettyprint prettyprinted" style=""><span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"runoob-1"</span><span class="pun">)</span><span class="pln">
console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"runoob-2"</span><span class="pun">)</span><button class="copy-code-button" type="button" data-clipboard-text="console.log(&quot;runoob-1&quot;)
console.log(&quot;runoob-2&quot;)
"></button></pre><p>
保存后，右击文件名，选择 "Run" 执行代码：</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2020/11/0DBBF606-1F97-4861-B690-1DBED83A0E5E.jpg"></p>			<!-- 其他扩展 -->
						
			</div>
			
		</div>